package com.yuyu.shop.infrastructure.persistence.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuyu.shop.domain.order.repository.TransactionalRecordRepository;
import com.yuyu.shop.infrastructure.persistence.mapper.TransactionalRecordMapper;
import com.yuyu.shop.infrastructure.persistence.po.TransactionalRecordPO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author
 * @version 1.0
 * @description: 分布式事务仓库实现类
 * @date 2022/12/12 13:17
 */
@Service
public class TransactionalRecordRepositoryImpl implements TransactionalRecordRepository {

    @Resource
    private TransactionalRecordMapper transactionalRecordMapper;
    @Override
    public TransactionalRecordPO getByXid(String xid) {
        LambdaQueryWrapper<TransactionalRecordPO> eq = new LambdaQueryWrapper<TransactionalRecordPO>().eq(TransactionalRecordPO::getXid, xid);
        return transactionalRecordMapper.selectOne(eq);
    }

    @Override
    public void insert(TransactionalRecordPO transactionalRecordPO) {
        TransactionalRecordPO one = transactionalRecordMapper.selectOne(new LambdaQueryWrapper<TransactionalRecordPO>()
                .eq(TransactionalRecordPO::getXid, transactionalRecordPO.getXid()));
        if (Objects.nonNull(one)) {
            transactionalRecordMapper.insert(transactionalRecordPO);
        }
    }
}
